package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.acticle.service.IAnswerService;
import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.vo.AnswerStatisVO;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.key.RedisKeys;
import cn.wolfcode.luowowo.cache.service.IAnswerRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyRedisService;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.member.vo.UserCommentVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis数据持久化
 */
@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyRedisService strategyRedisService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IAnswerRedisService answerRedisService;
    @Reference
    private IAnswerService answerService;
    @Reference
    private IUserInfoService userInfoService;

    @Scheduled(cron = "0/20 * * * * ? ")
    public void redisDataPersistence(){
        System.err.println("----- redisDataPersistence job begin ---");
        //数据落地
        //获取redis中所有vo数据
        List<StrategyStatisVO> voS = strategyRedisService.getVoByPattern(RedisKeys.STRATEGY_STATIS_VO.join("*"));

        for (StrategyStatisVO vo : voS) {
            StrategyStatisVO vo1 = new StrategyStatisVO();
            BeanUtil.copyProperties(vo1,vo);
            strategyDetailService.updateStatisByVo(vo1);
        }
        System.err.println("----- redisDataPersistence job end ---");
    }

    @Scheduled(cron = "0/12 * * * * ? ")
    public void redisDataPersistenceByAnswer(){
        System.err.println("----- redisDataPersistenceByAnswer job begin ---");
        //数据落地
        //获取redis中所有vo数据
        List<AnswerStatisVO> voS = answerRedisService.getVoByPattern(RedisKeys.ANSWER_STATIS_VO.join("*"));

        for (AnswerStatisVO vo : voS) {
            AnswerStatisVO vo1 = new AnswerStatisVO();
            BeanUtil.copyProperties(vo1,vo);
            answerService.updateStatisByVo(vo1);
        }
        System.err.println("----- redisDataPersistenceByAnswer job end ---");
    }

    @Scheduled(cron = "0/10 * * * * ? ")
    public void redisDataPersistenceByUserComment(){
        System.err.println("----- redisDataPersistenceByUserComment job begin ---");
        //数据落地
        //获取redis中所有vo数据
        List<UserCommentVO> voS = answerRedisService.getVoByUserPattern(RedisKeys.USER_STATIS_VO_COMMENT.join("*"));

        for (UserCommentVO vo : voS) {
            UserCommentVO vo1 = new UserCommentVO();
            BeanUtil.copyProperties(vo1,vo);
            userInfoService.updateStatisByVo(vo1);
        }
        System.err.println("----- redisDataPersistenceByUserComment job end ---");
    }
}
